<?php
namespace app\api\controller;
use app\Response;
use QL\QueryList;
use \Curl\Curl;
use \Curl\MultiCurl;
use think\Db;
use think\Config;

class FileList {
	public function index(){
		//系统环境
		$system = self::system();

		$img_path = Config::get('img_path');
		$path = isset($img_path['root_dir'])?$img_path['root_dir']:'';
		$web = isset($img_path['web'])?$img_path['web']:'';

		if($system == 'windows'){
			$system_path = self::encoding($path, "GBK", "UTF-8");
		}else{
			$system_path = $path;
		}
		
		$file = self::getDir($system_path);
		
		$data = array();
		if($file){
			foreach ($file as $key => $val) {
				$dir_path = self::encoding($val);

				if(stristr($dir_path, 'file/')){
					$dir_path_Arr = explode('file/', $dir_path);
					$link = isset($dir_path_Arr[1])?'/file/'.$dir_path_Arr[1]:'';
				}else{
					$link = $dir_path;
				}
				
				$filemtime = filemtime($val);
				$filemtime = date('Y-m-d H:i:s', $filemtime);

				$file_Arr = explode('/', $dir_path);
				$exfile = isset($file_Arr[count($file_Arr)-1])?$file_Arr[count($file_Arr)-1]:$dir_path;
				$file_Arr1 = explode('.', $exfile);

				$file_name = isset($file_Arr1[0])?$file_Arr1[0]:'未命名';
				$ext = isset($file_Arr1[1])?$file_Arr1[1]:'类型未知';
				$data[] = array('name' => $file_name, 'type' => $ext,'filemtime' => $filemtime, 'link' => $link, 'dir_path' => $dir_path);	
			}
		}
		$result = array('info' => $data);
		Response::show(200,'ok',$result);
	}
	/**
	 * [rename 重命名文件]
	 * @return [type] [description]
	 * me.com/api/list/rename
	 */
	public function rename(){
		//系统环境
		$system = self::system();
		
		$path = isset($_GET['path'])?$_GET['path']:'';
		$name = isset($_GET['name'])?$_GET['name']:'';
		$rename = false;
		if($path && $name){
			$path_Arr = explode('.', $path);
			$total = count($path_Arr);
			$ext = isset($path_Arr[$total-1])?$path_Arr[$total-1]:'txt';
			$file = isset($path_Arr[$total-2])?$path_Arr[$total-2]:'';
			//排除文件名的路径
			$file_path = '';
			if($file){
				$file_Arr = explode('/', $file);
				$old_name = isset($file_Arr[count($file_Arr)-1])?$file_Arr[count($file_Arr)-1]:'';
				$length = mb_strlen($old_name);
				$all_length = mb_strlen($file);
				$file_path = mb_substr($file,0,$all_length-$length);
			}
			$new_name = $file_path.$name.'.'.$ext;
			$new_path = $new_name;
			if($system == 'windows'){
				$path = self::encoding($path, "GBK", "UTF-8");
				$new_name = self::encoding($new_name, "GBK", "UTF-8");
			}

			$rename = rename($path,$new_name);
		}
		if($rename){
			$result = array('path' => $new_path);
			Response::show(200,'重命名成功',$result);
		}else{
			Response::show(-201,'重命名失败');
		}
	}
	/**
	 * [delete 删除文件]
	 * @return [type] [description]
	 */
	public function delete(){
		$path = isset($_GET['path'])?$_GET['path']:'';
		$unlink = false;
		if($path){
			$path = self::encoding($path, "GBK", "UTF-8");
			$unlink = unlink($path);
		}
		if($unlink){
			Response::show(200,'删除成功');
		}else{
			Response::show(-201,'删除失败');
		}	
	}
	//me.com/api/list/recordVideo
	public function recordVideo(){
		//系统环境
		$system = self::system();

		$video_path = Config::get('video_path');
		$path = isset($video_path['root_dir'])?$video_path['root_dir']:'';
		$web = isset($video_path['web'])?$video_path['web']:'';

		if($system == 'windows'){
			$system_path = self::encoding($path, "GBK", "UTF-8");
		}else{
			$system_path = $path;
		}

		$file = self::getDir($system_path);
		
		$result = array();
		if($file){
			foreach ($file as $key => $val) {
				$data = array();
				$dir_path = self::encoding($val);

				if(stristr($val, 'file/')){
					$dir_path_Arr = explode('file/', $dir_path);
					$link = isset($dir_path_Arr[1])?'/file/'.$dir_path_Arr[1]:'';
				}else{
					$link = $dir_path;
				}
				
				$filemtime = filemtime($val);
				$filemtime = date('Y-m-d H:i:s', $filemtime);
				$file_size = round(filesize($val)/1024/1024);

				$file_Arr = explode('/', $dir_path);
				$exfile = isset($file_Arr[count($file_Arr)-1])?$file_Arr[count($file_Arr)-1]:$dir_path;
				$file_Arr1 = explode('.', $exfile);

				$file_name = isset($file_Arr1[0])?$file_Arr1[0]:'';
				$ext = isset($file_Arr1[1])?$file_Arr1[1]:'';

				//获取文件mime_type
				$finfo = finfo_open(FILEINFO_MIME);
				$mimetype = finfo_file($finfo, $val);
				$type_Arr = explode(';', $mimetype);
				$mime_type = isset($type_Arr[0])?$type_Arr[0]:'';
				finfo_close($finfo);

				$data = array('name' => $file_name, 'mime_type' => $mime_type,'ext' => $ext,'file_time' => $filemtime, 'web' => $link, 'path' => $dir_path, 'size' => $file_size, 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s'));
				$addData = self::addData($data);
				$result[] = $data;	
			}
		}

		Response::show(200,'ok',$result);
	}
	/**
	 * [addData 插入数据]
	 * @param [type] $data [description]
	 */
	public static function addData($data){
		$name = isset($data['name'])?$data['name']:'';
		$path = isset($data['path'])?$data['path']:'';
		$id = 0;
		if($name && $path){
			//校验数据是否已存在
			$is_save = Db::table('tbl_video')
				    ->where('name',$name)
				    ->where('path',$path)
				    ->find();
			if(!$is_save){
				$id = Db::table('tbl_video')->insertGetId($data);
			}	    
		}
		return $id;
	}
	/**
	 * [getVideo 获取视频地址]
	 * @return [type] [description]
	 */
	public function getVideo(){
		//$host = 'http://'.$_SERVER['HTTP_HOST'];
		$video_path = Config::get('video_path');
		$path = isset($video_path['root_dir'])?$video_path['root_dir']:'';
		$web = isset($video_path['web'])?$video_path['web']:'';

		$source = "/file/video/mm.mp4";
		$mime = "video/mp4";

		$file_Arr = explode('/', $source);
		$file_name = $file_Arr[count($file_Arr)-1];
		$file = $path.'/'.$file_name;

		$finfo = finfo_open(FILEINFO_MIME);
		$mimetype = finfo_file($finfo, $file);
		finfo_close($finfo);
		
		$result = array('source' => $source, 'mime' => $mimetype);
		Response::show(200,'ok', $result);	
	}
	/**
	 * [encoding 编码转化]
	 * @param  string $str           [要转化的字符]
	 * @param  string $encoding_to   [要转化的目的编码]
	 * @param  string $encoding_from [原编码]
	 * @return [type]                [description]
	 */
	public static function encoding($str='', $encoding_to="UTF-8", $encoding_from="GBK"){
		if($str){
			//已知原编码为encoding_from，转换为encoding_to
			$str = mb_convert_encoding($str, $encoding_to, $encoding_from); 
		}
		return $str;
	}
	public static function system(){
		$system = strtoupper(substr(PHP_OS,0,3))==='WIN'?'windows':'linux';
		return $system;
	}
	/**
	 * [getDir 获取指定文件夹下文件]
	 * @param  [type] $dir [description]
	 * @return [type]      [description]
	 */
	public static function getDir($dir){
		$data=array();
		self::searchDir($dir,$data);
		return   $data;
	}
	public static function searchDir($path,&$data){
		if(is_dir($path)){
			$dp=dir($path);
			while($file=$dp->read()){
				if($file!='.'&& $file!='..'){
					self::searchDir($path.'/'.$file,$data);
				}
			}
			$dp->close();
		}
		if(is_file($path)){
			$data[]=$path;
		}
	}
}